package com.ty.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ty.dao.CustomerDao;
import com.ty.dao.OrdersDao;
import com.ty.entity.Customer;
import com.ty.entity.Orders;
import com.ty.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * (Orders)表服务实现类
 *
 * @author makejava
 * @since 2020-12-01 22:17:53
 */
@Service("ordersService")
public class OrdersServiceImpl extends ServiceImpl<OrdersDao, Orders> implements OrdersService {

    @Autowired
    private OrdersDao ordersDao;
    @Autowired
    private CustomerDao customerDao;

    @Override
    public List<Orders> queryByMobileIsOrdersCustomer(String mobile) {
        return ordersDao.queryByMobileIsOrdersCustomer(mobile);
    }

    @Override
    public PageInfo<Orders> getPage(Orders orders, Integer page, Integer limit) {
        //开启分页
        PageHelper.startPage(page, limit);
        //查询数据
        List<Orders> list = this.ordersDao.getPage(orders);
        //封装到PageInfo中
        PageInfo<Orders> pageInfo = new PageInfo<Orders>(list);
        return pageInfo;
    }

    @Override
    public boolean isCompletedOrderByPhone(String phone) {
        Orders orders = new Orders();
        orders.setPhone(phone);
        List<Orders> list = this.ordersDao.queryAll(orders);
        AtomicBoolean flag = new AtomicBoolean(true);
        list.forEach(order -> {
            //未完成
            if ("0".equals(order.getStatus()) || "1".equals(order.getStatus()) || "2".equals(order.getStatus())){
                flag.set(false);
            }
        });
        return flag.get();
    }

    @Override
    public boolean saveOrderCustoner(Customer customer, Orders orders) {

        //TODO 加上事务管理,现在默认返回true

        Customer customer1 = new Customer();
        customer1.setName(customer.getName());
        customer1.setPhone(customer.getPhone());
        customer1.setCreatetime(new Date());
        customer1.setScore("0");
        customer1.setCar(customer.getCar());
        customer1.setSex("男");
        this.customerDao.insert(customer1);
        Orders orders1 = new Orders();
        orders1.setPhone(orders.getPhone());
        orders1.setLng(orders.getLng());
        orders1.setLat(orders.getLat());
        orders1.setCreatetime(new Date());
        orders1.setStatus("0");
        orders1.setAddress(orders.getAddress());
        orders1.setCost(orders.getCost());
        this.baseMapper.insert(orders1);
        return true;
    }

    @Override
    public List<Orders> queryAll(Orders orders) {
        return ordersDao.queryAll(orders);
    }

    @Override
    public Integer updateOrder(Orders orders) {
        return this.ordersDao.updateOrder(orders);
    }
}